<?php
require_once 'pages/TMSAdminPage.inc';
require_once 'excel/Classes/PHPExcel.php';
require_once 'datamining/TMSDataMine.inc';

/**
 * @author kras
 * @property TMSDataMine $DataMine
 */
class TMSReportsPage extends TMSAdminPage {
/**
 * @var TMSDataMine
 */	
	protected $_ioc_data_mine_;
	
	public function Dimensions(){
		return $this->DataMine->AvailableDimensions();
	}
	
	public function http_Pivot($since = null,$till = null, array $dimensions = array()){		
		if ($since)
			$since = new TDate($since);
		else {
			$since = new TDate();
			$since = $since->Add(-1,TDate::DP_MONTH);
		}
		
		if ($till)
			$till = new TDate($till);
		else
			$till = new TDate();
		
		$ad = $this->DataMine->AvailableDimensions();
		
		$dim = array();
		$dn = 0;
		$dimcap = array();
		foreach ($dimensions as $c=>$d){
			$dim[$c] = array_keys($d);
			$dn += count($dim[$c]);
			foreach ($dim[$c] as $nm)
				$dimcap[] = $ad[$c]['dimensions']->$nm->Caption;
		}
		
		$result = $this->DataMine->Pivot($since, $till, $dim);
		
		// Create new PHPExcel object
		$objPHPExcel = new PHPExcel();
		
		// Set document properties
		$objPHPExcel->getProperties()->setCreator($this->Application()->Host())
		->setLastModifiedBy($this->Application()->Host())
		->setTitle("Отчет по продажам")
		->setDescription("");
		
		
		// Add some data
		$sheet = $objPHPExcel->setActiveSheetIndex(0);
		
		$row = 1;
		$sheet->setCellValueExplicitByColumnAndRow(0,$row,'Date',PHPExcel_Cell_DataType::TYPE_STRING);
		$sheet->setCellValueExplicitByColumnAndRow(1,$row,'Client',PHPExcel_Cell_DataType::TYPE_STRING);
		$sheet->setCellValueExplicitByColumnAndRow(2,$row,'Category',PHPExcel_Cell_DataType::TYPE_STRING);
		$sheet->setCellValueExplicitByColumnAndRow(3,$row,'Price group',PHPExcel_Cell_DataType::TYPE_STRING);
		for($i = 0; $i < $dn; $i++)
			$sheet->setCellValueExplicitByColumnAndRow(4+$i,$row,$dimcap[$i],PHPExcel_Cell_DataType::TYPE_STRING);
		$sheet->setCellValueExplicitByColumnAndRow(4+$i,$row,'Quantity',PHPExcel_Cell_DataType::TYPE_STRING);
		$sheet->setCellValueExplicitByColumnAndRow(5+$i,$row,'Order amount',PHPExcel_Cell_DataType::TYPE_STRING);
		
		$row++;
		foreach ($result as $r){
			$sheet->setCellValueExplicitByColumnAndRow(0,$row,$r['added'],PHPExcel_Cell_DataType::TYPE_STRING);
			$sheet->setCellValueExplicitByColumnAndRow(1,$row,$r['loyal']?'registered':'anonymous',PHPExcel_Cell_DataType::TYPE_STRING);
			$sheet->setCellValueExplicitByColumnAndRow(2,$row,$r['category'],PHPExcel_Cell_DataType::TYPE_STRING);
			$sheet->setCellValueExplicitByColumnAndRow(3,$row,$r['price_group'],PHPExcel_Cell_DataType::TYPE_NUMERIC);
			for($i = 0; $i < $dn; $i++)
				$sheet->setCellValueExplicitByColumnAndRow(4+$i,$row,$r['dim'.$i],PHPExcel_Cell_DataType::TYPE_STRING);
			$sheet->setCellValueExplicitByColumnAndRow(4+$i,$row,$r['quantaty'],PHPExcel_Cell_DataType::TYPE_NUMERIC);
			$sheet->setCellValueExplicitByColumnAndRow(5+$i,$row,$r['amount'],PHPExcel_Cell_DataType::TYPE_NUMERIC);
			$row++;
		}
		
		// Rename worksheet
		$objPHPExcel->getActiveSheet()->setTitle('Pivot');
		
		
		// Set active sheet index to the first sheet, so Excel opens this as the first sheet
		$objPHPExcel->setActiveSheetIndex(0);
		
		
		// Redirect output to a client’s web browser (Excel2007)
		
		$filename = urlencode(str_replace(array('<','>',':','"','/','\\','|','?','*'), '', 'Отчет по продажам'));
		
		header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8');
		header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
		header('Cache-Control: max-age=0');
		
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
		$objWriter->save('php://output');		
		die;
	}
}